package com.asktgapp.user.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.asktgapp.R;
import com.asktgapp.application.ApiUtil;
import com.asktgapp.application.Apiservice;
import com.asktgapp.architecture.retrofit.ApiException;
import com.asktgapp.architecture.retrofit.ApiResponseBody;
import com.asktgapp.architecture.rxjava.ExceptionHandle;
import com.asktgapp.base.BaseFragment;
import com.asktgapp.modulebase.common.util.PreferencesUtil;
import com.asktgapp.user.activity.EditNickActivity;
import com.asktgapp.user.activity.MainActivity;
import com.asktgapp.utils.ImageDisplayUtil;
import com.asktgapp.utils.SelectHeardPhotoDialogFragment;
import com.jph.takephoto.model.TImage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static android.app.Activity.RESULT_OK;

/**
 * time:2018/2/2
 * Create By ZhaoKai
 * Email: dczk@foxmail.com
 * Description:
 */

public class PersonalInfoFragment extends BaseFragment {

    @InjectView(R.id.rl_head)
    RelativeLayout mHeadRl;
    @InjectView(R.id.iv_head)
    ImageView mHead;
    @InjectView(R.id.tv_nick)
    TextView mNick;
    private List<String> uri;
    private String name;
    private String headUrl;
    List<Uri> mSelected;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_edit_info, container, false);
        ButterKnife.inject(this, viewGroup);
        return super.onCreateView(inflater, viewGroup, savedInstanceState);
    }

    @Override
    protected void initView(View rootView) {
        getBaseActivity().setBackNavigationIcon();
        mHeadRl.setOnClickListener(this);
        mNick.setOnClickListener(this);
        uri = new ArrayList<>();
        name = getActivity().getIntent().getStringExtra("name");
        headUrl = getActivity().getIntent().getStringExtra("head");
        ImageDisplayUtil.loadCircleHeadImage(getActivity(), mHead, headUrl);
        mNick.setText(name);

    }

    @Override
    public void loadData() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.rl_head:
                SelectHeardPhotoDialogFragment selectPhotoDialogFragment = new SelectHeardPhotoDialogFragment(new SelectHeardPhotoDialogFragment.setResult() {
                    @Override
                    public void setResult(ArrayList<TImage> mImages) {
                        uri.clear();
                        String path = mImages.get(0).getPath();
                        uri.add(path);
                        ImageDisplayUtil.loadCircleHeadImage(getActivity(), mHead, path);
                        UpdateHeadImg();

                    }
                }, 1);
                selectPhotoDialogFragment.show(getFragmentManager(), "selectPhotoDialogFragment");


                break;
            case R.id.tv_nick:
                if (PreferencesUtil.getBoolean(getActivity(), PreferencesUtil.KEY_IS_ENGINEER)) {
                    showTost("您已认证工程师，不可修改昵称",0);
                }else {
                    Intent intent = new Intent(getActivity(), EditNickActivity.class);
                    intent.putExtra("nick", mNick.getText().toString());
                    startActivityForResult(intent, 100);
                }
                break;
        }
    }

    private void UpdateHeadImg() {
        Apiservice apiservice = ApiUtil.getInstance().create();
        HashMap params = new HashMap();
        ApiUtil.putDefultData(getActivity(), params);
        showProgress(getClass().getSimpleName());
        Call<ApiResponseBody> call = apiservice.updateHeadIMG(params, ApiUtil.files2Parts("userPic", uri.toArray(), ApiUtil.MULTIPART));
        call.enqueue(new Callback<ApiResponseBody>() {
            @Override
            public void onResponse(Call<ApiResponseBody> call, Response<ApiResponseBody> response) {
                dismissProgress();
                if (response.isSuccessful()) {
                    showTost("头像修改成功", 0);
                } else {
                    showTost(response.raw().message(), 0);
                }
            }

            @Override
            public void onFailure(Call<ApiResponseBody> call, Throwable t) {
                dismissProgress();
                ApiException apiException = ExceptionHandle.handleException(t);
                if (apiException.isTokenInvalid()) {
//                    showUnLoginSnackbar();
                } else if (apiException.isNetConnected()) {
                    showSetNetworkSnackbar();
                } else {
                    showTost(apiException.getMessage(), 1);
                }
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == 100) {
            String name = data.getStringExtra("nick");
            mNick.setText(name);
        }
//        if (requestCode == 200 && resultCode == RESULT_OK) {
//            mSelected = Matisse.obtainResult(data);
//            Log.i("Matisse", "mSelected: " + mSelected);
//        }
    }


}
